<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
  import { ref, Ref, watch } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { basicProps } from './props';
  import { BarChartDataItem } from '../indexInsurance.data';

  const props = defineProps({
    ...basicProps,
    data: {
      type: Array<BarChartDataItem>,
      default: () => [],
    },
    numData: {
      type: Array<BarChartDataItem>,
      default: () => [],
    },
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  watch(
    () => props.data,
    () => {
      if (!props.data || props.data.length === 0) {
        return;
      }
      const xAxisData = props.data.map((item) => item.tag);
      const seriesData = props.data.map((item) => item.value);
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 1,
              color: '#019680',
            },
          },
          formatter: function (params) {
            // params 是一个数组，数组中包含每个系列的数据信息
            const item = params[0] || {};
            let result = item.axisValue + '<br/>';
            const seriesIndex = item.dataIndex;
            const value = item.value; // 数据值
            const marker = item.marker; // 标志图形
            result += `${marker}保费: ${value}<br/>`;
            const number = props.numData[seriesIndex].value;
            result += `${marker}保单: ${number}`;
            return result;
          },
        },
        grid: { left: '1%', right: '1%', top: '2  %', bottom: 0, containLabel: true },
        xAxis: {
          type: 'category',
          data: xAxisData,
        },
        yAxis: {
          type: 'value',
          splitNumber: 4,
        },
        series: [
          {
            data: seriesData,
            type: 'bar',
            barMaxWidth: 80,
          },
        ],
      });
    },
    { immediate: true }
  );
</script>
